# -*- coding:utf8 -*-

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from keras.layers.advanced_activations import *
from data_prepare.load_data import load_hdf5_data_train,load_hdf5_data_test
import matplotlib.pyplot as plt
import os
import Image
import csv

def Alex_like_predict_noAug_96():
    nb_classes = 40
    data_augmentation = False

    # shape of the image (SHAPE x SHAPE)
    img_rows, img_cols = 96, 96

    # the CIFAR10 images are RGB
    img_channels = 3

    print('Model Average: Alex')

    # the data, shuffled and split between tran and test sets
    X_test = load_hdf5_data_test(dataset='/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/submit/testVec96all')

    print('X_test shape:', X_test.shape)
    print(X_test.shape[0], 'test samples')

    model = Sequential()

    model.add(Convolution2D(96, 5, 5, input_shape=(img_channels, img_rows, img_cols), init='he_normal', subsample=(2, 2)))
    model.add(Activation('relu'))

    model.add(Convolution2D(256, 5, 5, init='he_normal'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Convolution2D(384, 3, 3, init='he_normal'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Convolution2D(384, 3, 3, init='he_normal'))
    model.add(Activation('relu'))

    model.add(Convolution2D(384, 3, 3, init='he_normal'))
    model.add(Activation('relu'))

    model.add(Flatten())

    model.add(Dense(4096, init='he_normal'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(2048, init='he_normal'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))

    # let's train the model using SGD + momentum (how original).
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    model.load_weights('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/Weights/submit/96/Test_newD_heNor_e50.hdf5')
    if not data_augmentation:
        print('using Relu activation')
        X_test = X_test.astype("float32")
        X_test /= 255

        Pred = model.predict(X_test)
        names = []
        for img in os.listdir('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/Proc/Test_all'):
            names.append(img)

        if len(Pred) != len(names):
            print 'submit fail'

        return names, Pred
